package git.soulbgm.common.serializer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import git.soulbgm.common.base.BaseSerializer;

import java.io.IOException;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2019/11/29 16:54
 */
public class JacksonSerializer implements BaseSerializer {

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public <T> T deserialize(Object val, Class<T> tClass) {
        try {
            return mapper.readValue(String.valueOf(val), tClass);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public Object serialize(Object val) {
        try {
            return mapper.writeValueAsString(val);
        } catch (JsonProcessingException e) {
            return null;
        }
    }
}
